﻿AJXFU = {
 
	frame : function(c,cid) {
 
		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AJXFU.loaded(\''+n+'\',\''+cid+'\')"></iframe>';
		document.body.appendChild(d);
 
		var i = document.getElementById(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}
 
		return n;
	},
 
	form : function(f, name) {
		f.setAttribute('target', name);
	},
 
	submit : function(f, c, cid) {
		AJXFU.form(f, AJXFU.frame(c,cid));
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},
 
	loaded : function(id,cid) {
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		if (d.location.href == "about:blank") {
			return;
		}
 
		if (typeof(i.onComplete) == 'function') {
		    var y = d.body.innerHTML;
		    var ret = "";
		    if( y.toUpperCase().replace("<PRE","") != y.toUpperCase() )
		    {
		        ret = d.body.innerText || d.body.textContent;
		    }
		    else
		    {
		        ret = d.body.innerHTML;
		    }
			
			i.onComplete(cid,ret);
		}
	}
 
}

setHiddenTarget = function(id,ashx){
    var formut = document.getElementsByTagName("FORM")[0];
    var lovname = $id("lovn_"+id).value;
    var URL = ashx + "?cid=" + escape(id) + "&lovname=" + lovname;
	formut.setAttribute('action', URL);
    formut.onsubmit=function() {
        return AJXFU.submit(this, {'onStart' : startUpload(id), 'onComplete' : completeUpload }, id);
    }
}

startUpload = function(id)
{

    var input = $id(id);
    //input.disabled = true;
    var div = $id("uplimg_" + id);
    var btn = $id("uplbtn_" + id);
    div.innerHTML = "<img src='" + LOADING.src + "' alt='' />";
    
    input.style.display = "none";
    btn.style.display = "none";
}

htmlDecode = function(input){
//  var e = document.createElement('div');
//  e.innerHTML = input;
//  return e.childNodes[0].nodeValue;
    return input.replace("&gt;", ">").replace("&lt;","<");
}

completeUpload = function(cid,responseText)
{
    var input = $id(cid);
    input.parentNode.innerHTML = responseText;
}

limitAnyExtension = function(value, idButton){
    var submitEl = document.getElementById(idButton);

    if (value != "") {
        submitEl.style.display = "";
        return true;
    } 
    else {
        alert("Please select a file");
        submitEl.style.display = "none";
        return false;
    }
}

limitImageExtension = function(value, idButton){
    var hash = {
      'PNG'  : 1,
      'JPG' : 1,
      'JPEG'  : 1,
      'BMP' : 1,
      'TIF'  : 1
    };
    
    return limitFileExtension(value, idButton, hash);
}

limitDocExtension = function(value, idButton){
    var hash = {
      'PDF'  : 1,
      'XLS'  : 1,
      'XLSX' : 1,
      'DOC'  : 1,
      'DOCX' : 1,
      'PPT'  : 1,
      'PPTX' : 1,
      'MPP'  : 1,
      'VSD'  : 1,
      'RTF' : 1,
      'TXT' : 1,
      'PNG'  : 1,
      'JPG' : 1,
      'JPEG'  : 1,
      'BMP' : 1,
      'TIF'  : 1
    };
    
    return limitFileExtension(value, idButton, hash);
    
}

limitFileExtension = function(value, idButton, hashtable){
    var submitEl = document.getElementById(idButton);

    if(value == "")
    {
        submitEl.style.display = "none";
        //submitEl.disabled = true;
        return false;
    }
    
    var re = /\..+$/;
    var ext = value.toUpperCase().match(re);
    
    
    if (hashtable[ext[0].replace(".","")]) {
        //submitEl.disabled = false;
        submitEl.style.display = "";
        return true;
    } 
    else {
        alert("Invalid file type, please select another file");
        //submitEl.disabled = true;
        submitEl.style.display = "none";
        return false;
    }
}

submitFormbyJS = function(id,ashx,action){
    var formut = document.getElementsByTagName("FORM")[0];
    var handlername = $id("lovn_"+id).value;
    var hf = $id("lovh_" + id);
    var filename = $id(id).value;
    
    if(filename == "")
    {
        return false;
    }
    
    var hf_value = "";
    
    if( hf != null )
    {
        hf_value = hf.value;
    }
    
    var URL = ashx + "?cid=" + escape(id) + 
        "&handlername=" + escape(handlername) + 
        "&value=" + escape(hf_value) + 
        "&action=" + escape(action);
        
	var tempaction = formut.getAttribute('action');

	formut.setAttribute('action', URL);

    if( AJXFU.submit(formut, {'onStart' : startUpload(id), 'onComplete' : completeUpload }, id) )
    {
        formut.submit();
    }
    
    if( tempaction)
    {
	    formut.setAttribute('action', tempaction);
	}
	else
	{
	    formut.setAttribute('action', "");	
	}
}

downloadUploaded = function(id){
    var value = $id(id).value;
    var lovname = $id("lovn_" + id).value;    
    var url = "ASHX/FileHandler.ashx?handlername=" + escape(lovname) +         "&action=down" +         "&cid=" + escape(id) +         "&value=" + escape(value);
        
    window.location = url;
}

downloadFile = function(idFile){
    var url = "ASHX/FileHandler.ashx?action=down" +         "&cid=dummy" +        "&value=" + escape(idFile);
        
    window.location = url;
}

deleteFile = function(idFile){
    var url = "ASHX/FileHandler.ashx?action=del" +         "&cid=dummy" +        "&value=" + escape(idFile);
        
    var ajx = sendAJAX(url,true);
    ajx.onreadystatechange = function() {        if (ajx.readyState == 4) {                    // WHAT TO DO?        }    }
}

deleteUploaded = function(id){
    if(!confirm("delete this file?"))
    {
        return;
    }
    
    var hf = $id("lovh_" + id);
    var value = $id(id).value;
    var lovname = $id("lovn_" + id).value;    var datatype = $id("lovdt_" + id);
    var input = $id(id);
    var dt = input.parentNode;    var datatypeid = datatype != null ? datatype.value : "";        dt.innerHTML = "<img src='" + LOADING.src + "' alt='' />";
    
    var url = "ASHX/FileHandler.ashx?handlername=" + escape(lovname) +         "&action=del" +         "&cid=" + escape(id) +         "&value=" + escape(value) +
        "&datatype=" + escape(datatypeid);
        
    var ajx = sendAJAX(url,true);
    ajx.onreadystatechange = function() {        if (ajx.readyState == 4) {                    var splitted = ajx.responseText.split(DELIMITER);            datatype.value = splitted[0];                        if( dt != null )            {                dt.innerHTML = splitted[1];            }        }    }
}